/**
 * 照片服务 - 封装照片相关的IPC调用
 */

import { Photo, PhotoListResponse, PhotoFilters, PhotoUpdateParams } from '../types'

/**
 * 照片服务类
 */
export class PhotoService {
  /**
   * 获取照片列表
   * @param params 查询参数
   * @returns 照片列表响应
   */
  static async getList(params?: {
    page?: number
    limit?: number
    orderBy?: string
    orderDirection?: 'ASC' | 'DESC'
    filters?: PhotoFilters
  }): Promise<PhotoListResponse> {
    const response = await window.electronAPI.photos.getList(params || {})
    
    if (!response.success) {
      throw new Error(response.error || '获取照片列表失败')
    }
    
    return response.data
  }

  /**
   * 获取照片详情
   * @param id 照片ID
   * @returns 照片详情
   */
  static async getById(id: number): Promise<Photo> {
    const response = await window.electronAPI.photos.getById({ id })
    
    if (!response.success) {
      throw new Error(response.error || '获取照片详情失败')
    }
    
    return response.data
  }

  /**
   * 更新照片信息
   * @param id 照片ID
   * @param updates 更新数据
   * @returns 更新后的照片信息
   */
  static async update(id: number, updates: PhotoUpdateParams): Promise<Photo> {
    const response = await window.electronAPI.photos.update({ id, updates })
    
    if (!response.success) {
      throw new Error(response.error || '更新照片失败')
    }
    
    return response.data
  }

  /**
   * 删除照片
   * @param id 照片ID
   * @returns 删除结果
   */
  static async delete(id: number): Promise<{ id: number }> {
    const response = await window.electronAPI.photos.delete({ id })
    
    if (!response.success) {
      throw new Error(response.error || '删除照片失败')
    }
    
    return response.data
  }

  /**
   * 获取照片缩略图路径
   * @param id 照片ID
   * @returns 缩略图路径
   */
  static async getThumbnail(id: number): Promise<string> {
    const response = await window.electronAPI.photos.getThumbnail({ id })
    
    if (!response.success) {
      throw new Error(response.error || '获取缩略图失败')
    }
    
    return response.data
  }

  /**
   * 监听导入进度事件
   * @param callback 进度回调函数
   */
  static onImportProgress(callback: (event: any, progress: any) => void): void {
    window.electronAPI.on('photos:import-progress', callback)
  }

  /**
   * 监听导入完成事件
   * @param callback 完成回调函数
   */
  static onImportComplete(callback: (event: any, result: any) => void): void {
    window.electronAPI.on('photos:import-complete', callback)
  }

  /**
   * 移除导入进度监听
   */
  static removeImportListeners(): void {
    window.electronAPI.off('photos:import-progress')
    window.electronAPI.off('photos:import-complete')
  }
}